package com.guoxin.mashibing.concurrent;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @author guoxin
 * @date 2019/7/11
 */
public class TicketSeller1 {

//    private static CopyOnWriteArrayList<String> tickets = new CopyOnWriteArrayList<>();
//    private static List<String> tickets = new ArrayList<>();
    private static Vector<String> tickets = new Vector<>();//同步容器

    static {
        IntStream.rangeClosed(1, 1000).forEach(num -> tickets.add("票编号：" + num));
    }


    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                while (tickets.size() > 0) {
                    System.out.println(Thread.currentThread().getName() + "，销售了--" + tickets.remove(0));
                    try {
                        TimeUnit.MILLISECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }, "窗口" + i).start();
        }
    }
}
